#include "NetLoginHandler.h"
#include "NetMsgHandler.h"

#include "CreatureManager.h"
#include "DBLoginHandler.h"

#include "ExpConfig.h"

#include "GameConfig.h"
#include "DBManager.h"


RECV_MSG_LOGIN_MAINSERVICE gMsgLoginMainService;
RECV_MSG_CREATEACCOUNT gMsgCreatePlayer;
RECV_MSG_CREATEROLE gMsgCreateRole;
RECV_MSG_ROLE_INFO gMsgRoleInfo;
RECV_MSG_MAPINFO gMsgMapInfo;
RECV_MSG_SYN_MAP_TEAM gMsgSynMapTeam;

RECV_MSG_ENTERMAP gMsgEnterMap;
RECV_MSG_PLAYERENTERMAP gMsgPlayerEnterMap;
RECV_MSG_EXITMAP gMsgExitMap;
RECV_MSG_NPCENTERMAP gMsgNpcEnterMap;
RECV_MSG_NPC_MOVE gMsgNpcMove;


extern dbIDatabase* gDBServer;

extern fvoid		DB_Guest( GamePlayer* player );

fint32		NetLoginHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_LOGIN_MAINSERVICE )
	
    if ( player->GetGUID() != INVALID_ID )
	{
		player->AddDanger( GAME_DESTRUCTIVELY_DANGER );
		FLOG0( "Player Login Twice %d" , player->GetGUID() );
        
		return GAME_NO_ERROR;
	}

	if ( msg->Guest )
	{
		DB_Guest( player );
		return GAME_NO_ERROR;
	}

	msg->Account[ MAX_NAME - 1 ] = '\0';
	msg->PassWord[ MAX_NAME - 1 ] = '\0';

	SPRINTF( SQL_BUFFER , SQL_SELECT_ACCOUNT_INFO.c_str() , msg->Account , msg->PassWord );
	SQL_BUFFER[ MAX_SQL_BUFFER - 1 ] = '\0';
	gDBServer->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Select_AccountInfo );
	
	return GAME_NO_ERROR;
}


fint32		NetCreateAccountHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_CREATEACCOUNT )

	msg->Account[ MAX_NAME - 1 ] = '\0';
	msg->PassWord[ MAX_NAME - 1 ] = '\0';
	msg->Name[ MAX_NAME - 1 ] = '\0';

	SPRINTF( SQL_BUFFER , SQL_INSERT_NEW.c_str() , msg->Account , msg->PassWord , msg->Name );
	SQL_BUFFER[ MAX_SQL_BUFFER - 1 ] = '\0';
	gDBServer->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Creat_Account );


	return GAME_NO_ERROR;
}


fint32		NetCreateRoleHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_CREATEROLE )
    
	if ( msg->Vitality + msg->Strength + msg->Toughness + msg->Dexterity > MAX_ROLE_BASESTATUS )
	{
		return GAME_NO_ERROR;
	}
	
	if ( msg->Earth + msg->Water + msg->Fire + msg->Wind > MAX_ROLE_ATTR )
	{
		return GAME_NO_ERROR;
	}

	if ( msg->Color > SARC_COLOR3 )
	{
		return GAME_NO_ERROR;
	}

	if ( msg->Role > MAX_ROLE_COUNT )
	{
		return GAME_NO_ERROR;
	}

	CreatureData& data = player->GetData();
	data.Vitality = msg->Vitality;
	data.Strength = msg->Strength;
	data.Toughness = msg->Toughness;
	data.Dexterity = msg->Dexterity;
	data.Level = 1;
	data.Base.Charm = 59;
	data.Base.Earth = msg->Earth * 10;
	data.Base.Water = msg->Water * 10;
	data.Base.Fire = msg->Fire * 10;
	data.Base.Wind = msg->Wind * 10;
	data.Color = msg->Color;
	data.Role = msg->Role;
	data.Eye = msg->Eye;
	data.Mouth = msg->Mouth;

	data.NEXTEXP = gExpConfig->EXP[ data.Level + 1 ];

	player->GetMoveData().Pos = gGameConfig->LoginMapPos;

	STRCPY( data.Name , msg->Name ) ;
	data.Name[ MAX_NAME - 1 ] = '\0';

	player->InitBaseData();
	player->UpdateFightData();

	player->GetFightData().Base.HP = player->GetFightData().Base.MAXHP;
	player->GetFightData().Base.MP = player->GetFightData().Base.MAXMP;

	SPRINTF( SQL_BUFFER , SQL_INSERT_ROLE.c_str() , player->Account , data.Name , DBManager::GetPlayerDBData( player ) );
	gDBServer->ExecuteAsyncSQL( SQL_BUFFER , player , DB_Creat_Role );
	
	return GAME_NO_ERROR;
}


fint32		NetGetRoleInfoHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_ROLE_INFO );

// 	GamePlayer* player = gCreatureManager->GetPlayer( socket );
// 
// 	gMsgRoleInfo.Data = player->GetData();
// 	gMsgRoleInfo.Pos = player->GetMoveData().Pos;
// 
// 	player->PackMsg( &gMsgRoleInfo );
// 	player->SendMsg();

	return GAME_NO_ERROR;
}

